package top.drinyeit.weilong.presenter;

import android.support.v7.widget.LinearLayoutManager;

import java.util.Map;

import top.drinyeit.weilong.activity.ChildrenOrderDetailActivity;
import top.drinyeit.weilong.adapter.ChildrenOrderDetailAdapter;
import top.drinyeit.weilong.app.Constant;
import top.drinyeit.weilong.model.ChildrenOrderDetailBean;
import top.drinyeit.weilong.retrofit.HttpResponse;
import top.drinyeit.weilong.retrofit.RetrofitMethod;
import top.drinyeit.weilong.retrofit.subscriber.CommonSubscriber;
import top.drinyeit.weilong.retrofit.subscriber.SubscriberListener;
import top.drinyeit.weilong.util.SPUtil;

import static top.drinyeit.weilong.app.Constant.COMMON_SUCCESS;

/**
 * Created by wu on 2018/1/12.
 */

public class ChildrenOrderDetailPresenter {
    private ChildrenOrderDetailActivity activity;
    private ChildrenOrderDetailAdapter childrenOrderDetailAdapter;

    public ChildrenOrderDetailPresenter(ChildrenOrderDetailActivity activity) {
        this.activity = activity;
    }

    //获取子订单详情
    public void getCOD(String order_id) {
        activity.showLoading();
        CommonSubscriber<HttpResponse<ChildrenOrderDetailBean>> subscriber = new CommonSubscriber<>(new SubscriberListener<HttpResponse<ChildrenOrderDetailBean>>() {
            @Override
            public void onError(String e, int code) {
                activity.dismissLoading();
                activity.showToastMsg(e);
            }

            @Override
            public void onNext(HttpResponse<ChildrenOrderDetailBean> o) {
                activity.dismissLoading();
                if (o.getCode() == COMMON_SUCCESS) {
                    activity.order_state.setText(getState(o.getData().getOrder_detail().getOrder_state()));
                    activity.consignee_name.setText(o.getData().getAddress_info().getConsignee_name());
                    activity.consignee_phone.setText(o.getData().getAddress_info().getConsignee_phone());
                    activity.address.setText(o.getData().getAddress_info().getAddress_detail_json().getProvince() + o.getData().getAddress_info().getAddress_detail_json().getCity() +
                            o.getData().getAddress_info().getAddress_detail_json().getRegion() + o.getData().getAddress_info().getAddress_detail_json().getDetail() + "");
                    childrenOrderDetailAdapter = new ChildrenOrderDetailAdapter(o.getData(),activity);
                    activity.mRecyclerView.setLayoutManager(new LinearLayoutManager(activity));
                    activity.mRecyclerView.setAdapter(childrenOrderDetailAdapter);
                    activity.mRecyclerView.setNestedScrollingEnabled(false);
                } else {
                    activity.showToastMsg(o.getMessage());
                }
            }
        });
        RetrofitMethod.getInstance().getCOD(subscriber, order_id);
    }

    private String getState(int state){
        switch (state){
            case 0:
                return "未支付";
            case 1:
                return "待配送";
            case 2:
                return "待签收";
            case 3:
                return "交易成功";
            default:
                return "订单已关闭";
        }
    }
}
